﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microhard.Permission.Interface;
using Microsoft.Practices.Unity;
using Microhard.Core.DBUtility;
using Microhard.WebUI.Models;
using Microhard.Core;
using Telerik.Web.Mvc;
using Microhard.Permission.MVCFilter;
using FluentValidation.Mvc;
using Microhard.Permission.MVCFilter.MVCFilter;
using Microhard.WeixinMP.Unities;
using Microhard.WeixinMP.Interfaces;
using Microhard.WeixinMP.Models;
using Microhard.Plat.CRM.Customers.Models;
using Microhard.Plat.CRM.Customers.Interface;


namespace Microhard.Plat.CRM.WebUI.Areas.Customers.Controllers
{
    public class SaleLeadController : MicroHardBaseController<SaleLeadModel>
    {
        private ISaleLead _resp = null;
        [Dependency]
        public ISaleLead Isl
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }
        }

        public ActionResult GetSaleLeadByUserName(string q, ScopeResult scope)
        {
            List<string> data = new List<string>();
            data.Add("张三，13005599209");
            data.Add("李四,14565432123");
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [MVCFilterAtrForReDirect(Order = 9, RedirectActionName = "Succeed", RedirectAreaName = "Orders", RedirectControllerName = "SaleLead", Condition = "mobileAdd")]
        public override ActionResult SaveCreate([CustomizeValidator(RuleSet = "Insert")]SaleLeadModel model, bool continueEditing, ScopeResult scope)
        {
            if (model.CustomerID != Guid.Empty)
            {
                ViewData["CustomerID"] = model.CustomerID;
            }
            return base.SaveCreate(model, continueEditing, scope);
        }

        /// <summary>
        /// 本接口使用微信OnAuth2.0用户数据获取
        /// </summary>
        /// <param name="wxmpID"></param>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [MVCFilterAtrForWeixinMP(true, Order = 1)]
        [MVCFilterAtrForLogon(Order = 2)]
        public ActionResult OnFocusMP(WeixinUserInfo WXUserInfo, ScopeResult scope)
        {
            var customerID = Guid.NewGuid();
            ICustomer ict = IoC.Resolve<ICustomer>();
            if (ict.IsExist(new CustomerModel { OpenID = WXUserInfo.openid }))
            {
                ViewData["Msg"] = "您已经是注册用户，不能再次注册";
            }
            else
            {
                var mc = new CustomerModel
                {
                    City = WXUserInfo.city,
                    CompanyID = WXUserInfo.BelongCompanyID,
                    CreateTime = DateTime.Now,
                    CustomerID = customerID,
              
                    NickName = WXUserInfo.nickname,
                    OpenID = WXUserInfo.openid,
                    Province = WXUserInfo.province,
                    privilege = WXUserInfo.privilege,
                    headimgurl = WXUserInfo.headimgurl
                };
                var rtn = ict.Add(mc);
                if (rtn.IsSucceed)
                {
                    ViewData["CustomerID"] = mc.CustomerID;
                }
                else
                {
                    ViewData["Msg"] = "程序处理错误，注册失败,错误:" + rtn.ErrorMsgTxt;
                }
            }
            return View("Create", new SaleLeadModel());
        }

        [MVCFilterAtrForWeixinMP(Order = 1)]
        [MVCFilterAtrForLogon(Order = 2)]
        public ActionResult Succeed()
        {
            return View();
        }

    }
}
